package com.dotto.atos.fragments;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.dotto.atos.R;
import com.dotto.atos.utils.MessageUtil;

public class BlueWikiFragment extends Fragment {
	public static final String TAG = "BlueWikiFragment";
	private View fragmentView;
	private Activity mActivity;
	@SuppressWarnings("unused")
	private BlueWikiFragment instance;

	@SuppressWarnings("unused")
	private MessageUtil messageUtil;

	private WebView wbBluewiki;
	private RelativeLayout loading_rl;
	private static final String BLUEWIKI_URL = "http://zen.myatos.net";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (fragmentView == null) {
			fragmentView = inflater.inflate(R.layout.fragment_bluewiki,
					container, false);
		} else {
			((ViewGroup) fragmentView.getParent()).removeView(fragmentView);
		}
		mActivity = this.getActivity();
		instance = this;
		messageUtil = new MessageUtil(mActivity);
		initView();
		return fragmentView;
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("SetJavaScriptEnabled")
	public void initView() {
		if (fragmentView != null) {
			loading_rl = (RelativeLayout) fragmentView
					.findViewById(R.id.loading_rl);

			wbBluewiki = (WebView) fragmentView.findViewById(R.id.wb_bluewiki);

			wbBluewiki.getSettings().setJavaScriptEnabled(true);
			wbBluewiki.getSettings().setSupportZoom(true);
			wbBluewiki.getSettings().setAppCacheEnabled(false);
			wbBluewiki.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
			wbBluewiki.getSettings().setAppCacheMaxSize(1);
			wbBluewiki.getSettings().setUseWideViewPort(true);
			wbBluewiki.getSettings().setBuiltInZoomControls(true);
			wbBluewiki.getSettings().setLoadWithOverviewMode(true);
			wbBluewiki.getSettings().setGeolocationEnabled(true);
			wbBluewiki.setWebChromeClient(new WebChromeClient() {

			});

			wbBluewiki.setWebViewClient(new WebViewClient() {

				@Override
				public void onPageStarted(WebView view, String url,
						Bitmap favicon) {
					super.onPageStarted(view, url, favicon);
				}

				@Override
				public void onPageFinished(WebView view, String url) {
					view.clearCache(true);
					loading_rl.setVisibility(View.INVISIBLE);
					super.onPageFinished(view, url);
				}
			});

			wbBluewiki.loadUrl(BLUEWIKI_URL);
		}
	}

}
